package com.demohomework.homeworkdemo.controller;


import com.demohomework.homeworkdemo.pojo.AssCorrect;
import com.demohomework.homeworkdemo.pojo.Result;
import com.demohomework.homeworkdemo.service.AssCorrectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/assCorrect")
public class AssCorrectController {
    @Autowired
    private AssCorrectService assCorrectService;

    //需要自己批改的作业
    @GetMapping
    public Result<List<AssCorrect>> list() {
        List<AssCorrect> assignments = assCorrectService.list();
        return Result.success(assignments);
    }

// 新增批改记录
@PostMapping("/add")
public Result add(@RequestBody @Validated(AssCorrect.Add.class) AssCorrect assCorrect) {
    // 检查是否存在相同的 assid 和 stuid
    Boolean exists = assCorrectService.existsByAssIdAndStuId(assCorrect.getAssId(), assCorrect.getStuId());
    // 安全判断 exists 的值
    if (Boolean.TRUE.equals(exists)) { // 如果 exists 为 true
        return Result.error("已批改过该作业！");
    }
    assCorrectService.add(assCorrect);
    return Result.success();
}


    //更新作业
    @PutMapping
    public Result update(@RequestBody @Validated(AssCorrect.Add.class) AssCorrect assCorrect){
        assCorrectService.update(assCorrect);
        return Result.success();
    }
    
}
